async-rate-limit
A basic Tokio rate-limiting library providing two traits and common implementations.
Usage:
use tokio::time::{Instant, Duration};
use async_rate_limit::limiters::VariableCostRateLimiter;
use async_rate_limit::sliding_window::SlidingWindowRateLimiter;
#[tokio::main]
async fn main() -> () {
let mut limiter = SlidingWindowRateLimiter::new(Duration::from_secs(1), 5);
for _ in 0..3 {
get_lite(&mut limiter).await;
}
get_heavy(&mut limiter).await;
}
async fn get_lite<T>(limiter: &mut T) where T: VariableCostRateLimiter {
limiter.wait_with_cost(1).await;
println!("Lite: {:?}", Instant::now());
}
async fn get_heavy<T>(limiter: &mut T) where T: VariableCostRateLimiter {
limiter.wait_with_cost(3).await;
println!("Heavy: {:?}", Instant::now());
}